package com.ruoyi.wzgl.wzpd.service.impl;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.wzgl.wzpd.domain.Wzpd;
import com.ruoyi.wzgl.wzpd.domain.WzpdPdmx;
import com.ruoyi.wzgl.wzpd.mapper.WzpdMapper;
import com.ruoyi.wzgl.wzpd.service.IWzpdService;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 物资盘点Service业务层处理
 *
 * @author huacai
 * @date 2024-09-28
 */
@Service
public class WzpdServiceImpl implements IWzpdService {
    @Resource
    private WzpdMapper wzpdMapper;

    @Resource
    private SqlSessionFactory sqlSessionFactory;

    /**
     * 查询物资盘点
     *
     * @param wzpdId 物资盘点主键
     * @return 物资盘点
     */
    @Override
    public Wzpd selectWzpdByWzpdId(Integer wzpdId) {
        return wzpdMapper.selectWzpdByWzpdId(wzpdId);
    }

    /**
     * 查询物资盘点列表
     *
     * @param wzpd 物资盘点
     * @return 物资盘点
     */
    @Override
    @DataScope(deptAlias = "d")
    public List<Wzpd> selectWzpdList(Wzpd wzpd) {
        return wzpdMapper.selectWzpdList(wzpd);
    }

    /**
     * 新增物资盘点
     *
     * @param wzpd 物资盘点
     * @return 结果
     */
    @Transactional
    @Override
    public int insertWzpd(Wzpd wzpd) {
        wzpd.setCreateTime(DateUtils.getNowDate());
        int rows = wzpdMapper.insertWzpd(wzpd);
        insertWzpdPdmx(wzpd);
        return rows;
    }

    /**
     * 批量新增物资盘点
     *
     * @param wzpds 物资盘点List
     * @return 结果
     */
    @Override
    public int batchInsertWzpd(List<Wzpd> wzpds) {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        int count = 0;
        if (!CollectionUtils.isEmpty(wzpds)) {
            try {
                for (int i = 0; i < wzpds.size(); i++) {
                    int row = wzpdMapper.insertWzpd(wzpds.get(i));
                    // 防止内存溢出，每100次提交一次,并清除缓存
                    boolean bool = (i > 0 && i % 100 == 0) || i == wzpds.size() - 1;
                    if (bool) {
                        sqlSession.commit();
                        sqlSession.clearCache();
                    }
                    count = i + 1;
                }
            } catch (Exception e) {
                e.printStackTrace();
                // 没有提交的数据可以回滚
                sqlSession.rollback();
            } finally {
                sqlSession.close();
                return count;
            }
        }
        return count;
    }

    /**
     * 修改物资盘点
     *
     * @param wzpd 物资盘点
     * @return 结果
     */
    @Transactional
    @Override
    public int updateWzpd(Wzpd wzpd) {
        wzpd.setUpdateTime(DateUtils.getNowDate());
        wzpdMapper.deleteWzpdPdmxByWzpdId(wzpd.getWzpdId());
        insertWzpdPdmx(wzpd);
        return wzpdMapper.updateWzpd(wzpd);
    }

    /**
     * 批量删除物资盘点
     *
     * @param wzpdIds 需要删除的物资盘点主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWzpdByWzpdIds(Integer[] wzpdIds) {
        wzpdMapper.deleteWzpdPdmxByWzpdIds(wzpdIds);
        return wzpdMapper.deleteWzpdByWzpdIds(wzpdIds);
    }

    /**
     * 删除物资盘点信息
     *
     * @param wzpdId 物资盘点主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWzpdByWzpdId(Integer wzpdId) {
        wzpdMapper.deleteWzpdPdmxByWzpdId(wzpdId);
        return wzpdMapper.deleteWzpdByWzpdId(wzpdId);
    }

    /**
     * 新增盘点明细信息
     *
     * @param wzpd 物资盘点对象
     */
    public void insertWzpdPdmx(Wzpd wzpd) {
        List<WzpdPdmx> wzpdPdmxList = wzpd.getWzpdPdmxList();
        Integer wzpdId = wzpd.getWzpdId();
        if (StringUtils.isNotNull(wzpdPdmxList)) {
            List<WzpdPdmx> list = new ArrayList<WzpdPdmx>();
            for (WzpdPdmx wzpdPdmx : wzpdPdmxList) {
                wzpdPdmx.setWzpdId(wzpdId);
                list.add(wzpdPdmx);
            }
            if (list.size() > 0) {
                wzpdMapper.batchWzpdPdmx(list);
            }
        }
    }
}
